/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hiruss.imageprocessor.ui;

import com.hiruss.imageprocessor.util.CannyEdgeDetector;
import com.hiruss.imageprocessor.util.Filters;
import com.hiruss.imageprocessor.util.ImageHandler;
import java.awt.image.BufferedImage;
import javax.swing.JOptionPane;

/**
 *
 * @author Hiru
 */
public class EdgeDetectionPanel extends javax.swing.JDialog {

    private UI parent;
    private static EdgeDetectionPanel instance;

    /**
     * Creates new form CropPanel
     */
    private EdgeDetectionPanel(UI parent, boolean modal) {
        super(parent, modal);
        this.parent = parent;
        initComponents();
        setLocationRelativeTo(parent);
    }

    public static EdgeDetectionPanel getInstance(UI parent) {
        if (EdgeDetectionPanel.instance == null) {
            EdgeDetectionPanel.instance = new EdgeDetectionPanel(parent, true);
        }
        instance.revertButton.setEnabled(false);
        return EdgeDetectionPanel.instance;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        resamplingMethodButtonGroup = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        applyButton = new javax.swing.JButton();
        revertButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        threasholdAvgTextField = new javax.swing.JTextField();
        sobelXFilterRadioButton = new javax.swing.JRadioButton();
        laplacianFilterRadioButton = new javax.swing.JRadioButton();
        sobelYFilterRadioButton = new javax.swing.JRadioButton();
        krishFilterRadioButton = new javax.swing.JRadioButton();
        cannyFilterRadioButton = new javax.swing.JRadioButton();
        useThreasholdFilterRadioButton = new javax.swing.JRadioButton();
        sobelBothFilterRadioButton = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Edge Detection");
        setBounds(new java.awt.Rectangle(0, 0, 400, 120));
        setMinimumSize(new java.awt.Dimension(620, 180));
        setModal(true);
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jPanel1.setLayout(new java.awt.GridLayout(1, 3, 6, 0));

        applyButton.setText("Apply");
        applyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                applyButtonActionPerformed(evt);
            }
        });
        jPanel1.add(applyButton);

        revertButton.setText("Revert");
        revertButton.setEnabled(false);
        revertButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                revertButtonActionPerformed(evt);
            }
        });
        jPanel1.add(revertButton);

        closeButton.setText("Close");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });
        jPanel1.add(closeButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(6, 12, 12, 12);
        getContentPane().add(jPanel1, gridBagConstraints);

        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel2.setLayout(new java.awt.GridBagLayout());

        threasholdAvgTextField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
        threasholdAvgTextField.setText("50");
        threasholdAvgTextField.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 6);
        jPanel2.add(threasholdAvgTextField, gridBagConstraints);

        resamplingMethodButtonGroup.add(sobelXFilterRadioButton);
        sobelXFilterRadioButton.setSelected(true);
        sobelXFilterRadioButton.setText("Sobel X");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(3, 6, 3, 3);
        jPanel2.add(sobelXFilterRadioButton, gridBagConstraints);

        resamplingMethodButtonGroup.add(laplacianFilterRadioButton);
        laplacianFilterRadioButton.setText("Laplacian");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        jPanel2.add(laplacianFilterRadioButton, gridBagConstraints);

        resamplingMethodButtonGroup.add(sobelYFilterRadioButton);
        sobelYFilterRadioButton.setText("Sobel Y");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(3, 6, 3, 3);
        jPanel2.add(sobelYFilterRadioButton, gridBagConstraints);

        resamplingMethodButtonGroup.add(krishFilterRadioButton);
        krishFilterRadioButton.setText("Krish");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        jPanel2.add(krishFilterRadioButton, gridBagConstraints);

        resamplingMethodButtonGroup.add(cannyFilterRadioButton);
        cannyFilterRadioButton.setText("Canny");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        jPanel2.add(cannyFilterRadioButton, gridBagConstraints);

        resamplingMethodButtonGroup.add(useThreasholdFilterRadioButton);
        useThreasholdFilterRadioButton.setText("Use Threashold");
        useThreasholdFilterRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                useThreasholdFilterRadioButtonStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        jPanel2.add(useThreasholdFilterRadioButton, gridBagConstraints);

        resamplingMethodButtonGroup.add(sobelBothFilterRadioButton);
        sobelBothFilterRadioButton.setText("Sobel Both");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(3, 6, 3, 3);
        jPanel2.add(sobelBothFilterRadioButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.2;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(12, 12, 6, 12);
        getContentPane().add(jPanel2, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void applyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_applyButtonActionPerformed

        setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
        applyButton.setEnabled(false);
        try {
            BufferedImage originalImage = parent.getEditingImage();
            BufferedImage destImage = parent.getEditingImage();

            if (sobelXFilterRadioButton.isSelected()) {
                destImage = ImageHandler.applyEdgeDetection(originalImage, Filters.TYPE.SOBEL_X);
            } else if (sobelYFilterRadioButton.isSelected()) {
                destImage = ImageHandler.applyEdgeDetection(originalImage, Filters.TYPE.SOBEL_Y);
            } else if (laplacianFilterRadioButton.isSelected()) {
                destImage = ImageHandler.applyEdgeDetection(originalImage, Filters.TYPE.LAPLACIAN);
            } else if (krishFilterRadioButton.isSelected()) {
                destImage = ImageHandler.applyEdgeDetectionKirsch(originalImage);
            } else if (sobelBothFilterRadioButton.isSelected()) {
                destImage = ImageHandler.applyEdgeDetection(originalImage, Filters.TYPE.SOBEL_BOTH);
            } else if (cannyFilterRadioButton.isSelected()) {
                destImage = CannyEdgeDetector.applyCannyEdgeDetector(originalImage);
            } else if (useThreasholdFilterRadioButton.isSelected()) {
                int threashold = Integer.parseInt(threasholdAvgTextField.getText());
                destImage = ImageHandler.applyThreshold(originalImage, threashold);
            }
            parent.setEditingImage(destImage);
            revertButton.setEnabled(parent.isRevertible());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this,
                    e.getLocalizedMessage(),
                    "Invalid Inputs!",
                    JOptionPane.WARNING_MESSAGE);
        }
        applyButton.setEnabled(true);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    }//GEN-LAST:event_applyButtonActionPerformed

    private void revertButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_revertButtonActionPerformed

        parent.revert();
//        parent.getEditingImageFrame().setFrameSize(tempEditingImage.getWidth(), tempEditingImage.getHeight());
        revertButton.setEnabled(false);
    }//GEN-LAST:event_revertButtonActionPerformed

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed

        dispose();
    }//GEN-LAST:event_closeButtonActionPerformed

    private void useThreasholdFilterRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_useThreasholdFilterRadioButtonStateChanged

        threasholdAvgTextField.setEnabled(useThreasholdFilterRadioButton.isSelected());
    }//GEN-LAST:event_useThreasholdFilterRadioButtonStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton applyButton;
    private javax.swing.JRadioButton cannyFilterRadioButton;
    private javax.swing.JButton closeButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton krishFilterRadioButton;
    private javax.swing.JRadioButton laplacianFilterRadioButton;
    private javax.swing.ButtonGroup resamplingMethodButtonGroup;
    private javax.swing.JButton revertButton;
    private javax.swing.JRadioButton sobelBothFilterRadioButton;
    private javax.swing.JRadioButton sobelXFilterRadioButton;
    private javax.swing.JRadioButton sobelYFilterRadioButton;
    private javax.swing.JTextField threasholdAvgTextField;
    private javax.swing.JRadioButton useThreasholdFilterRadioButton;
    // End of variables declaration//GEN-END:variables
}
